# include path
vpath %.S stlib
vpath %.c stlib stlib/src 
vpath %.h stlib stlib/cminc stlib/inc

DEFS += -DUSE_STDPERIPH_DRIVER

INCS += -Istlib -Istlib/cminc -Istlib/inc

# object files
OBJS += main.o
OBJS += stlib/startup_stm32f40xx.o 
OBJS += stlib/system_stm32f4xx.o
OBJS += stlib/src/stm32f4xx_syscfg.o stlib/src/stm32f4xx_gpio.o 

# customized peripheral device
OBJS += stlib/src/sys.o
OBJS += stlib/src/LED.o
OBJS += stlib/src/delay.o
OBJS += stlib/src/misc.o
OBJS += stlib/src/stm32f4xx_rcc.o
OBJS += stlib/src/stm32f4xx_usart.o
OBJS += stlib/src/usart.o
OBJS += stlib/src/printf.o

# compiler and linker flags
CFLAGS += -mcpu=cortex-m4 -mthumb -Wall 
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
CFLAGS += -Os
CFLAGS += -ffunction-sections -fdata-sections

LFLAGS += -mcpu=cortex-m4 -mthumb
LFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
LFLAGS += -Wl,--gc-sections 

# generation of hex file
all:app.bin

clean:
	@rm -f $(OBJS)
	@rm -f app.bin app.elf apps.bin apps.elf
	
app.bin:app.elf
	@arm-none-eabi-objcopy -O binary -S $< $@

app.hex: app.elf  
	@arm-none-eabi-objcopy -O ihex -S $< $@
	
app.elf:$(OBJS)
	@arm-none-eabi-gcc $(LFLAGS) $^ -Tstlib/STM32F407ZET6_FLASH.ld -o $@
	@arm-none-eabi-size $@
	

#burn:app.bin
#	@st-flash write $< 0x08000000
	
%.o:%.S
	@echo cc: $<
	@arm-none-eabi-gcc $(CFLAGS) -c $< -o $@
%.o:%.c
	@echo cc: $<
	@arm-none-eabi-gcc $(CFLAGS) $(DEFS) $(INCS) -c $< -o $@


	
